package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysRole;
import com.powernode.dto.SysRoleUpdateDto;
import com.powernode.model.Result;
import com.powernode.service.SysRoleService;
import com.powernode.vo.SysRoleInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/1/5 21:34
 */
@RestController
@Api("角色信息接口管理")
@RequestMapping("sys/role")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 查询所有角色信息
     * @return
     */
    @ApiOperation("查询所有角色信息")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public Result<List<SysRole>> loadList(){
        List<SysRole> sysRoles = sysRoleService.queryRoleList();
        return Result.success(sysRoles);
    }

    /**
     * 分页查询所有角色信息
     */
    @ApiOperation("分页查询所有角色信息")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public Result<Page> pageRole(@RequestParam(value = "current") Long current,
                                 @RequestParam(value = "size") Long size,
                                 @RequestParam(required = false) String roleName){

        Page<SysRole> sysRolePage = new Page<>();

        sysRolePage = sysRoleService.page(sysRolePage,
                new LambdaQueryWrapper<SysRole>()
                        .like(StringUtils.hasText(roleName), SysRole::getRoleName, roleName)
                        .orderByDesc(SysRole::getCreateTime));


        return Result.success(sysRolePage);
    }

    /**
     * 根据角色id获取角色信息
     * @param rid
     * @return
     */
    @ApiOperation("根据角色id获取角色信息")
    @GetMapping("info/{rid}")
    @PreAuthorize("hasAuthority('sys:role:info')")
    public Result<SysRoleInfoVo> loadRoleInfoByRid(@PathVariable(value = "rid") Long rid){

        return Result.success(sysRoleService.queryRoleInfoByrid(rid));
    }

    /**
     * 新增角色
     * @return
     */
    @ApiOperation("新增角色")
    @PostMapping()
    @PreAuthorize("hasAuthority('sys:role:save')")
    public Result<String> saveRole(@RequestBody SysRole sysRole){
        Integer count = sysRoleService.saveRole(sysRole);


        return Result.handle(count >= 1);
    }

    /**
     * 删除/批量删除角色
     * @return
     */
    @ApiOperation("删除/批量删除角色")
    @DeleteMapping()
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result<String> deleteRoleById(@RequestBody List<Long> RoleIds){
        Integer count = sysRoleService.removeRoleByIds(RoleIds);

        return Result.handle(count >= 1);
    }


    /**
     * 修改角色信息
     * @return
     */
    @ApiOperation("修改角色信息")
    @PutMapping()
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result<String> updateRoleInfo(@RequestBody SysRoleUpdateDto sysRoleUpdateDto){

        Integer count = sysRoleService.modifyRoleInfo(sysRoleUpdateDto);

        return Result.handle(count >= 1);
    }

}
